package com.km.team.phsystem.web.controller;

import com.km.team.phsystem.beans.pojo.BusinessTypeOptArticle;
import com.km.team.phsystem.beans.vo.ArticleVo;
import com.km.team.phsystem.beans.vo.ResponseData;
import com.km.team.phsystem.common.constants.Constants;
import com.km.team.phsystem.common.enums.ResultEnum;
import com.km.team.phsystem.web.controller.base.BaseController;
import com.km.team.phsystem.web.utils.ResponseDataUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author: jiangjun
 * Date: 2019-01-19
 * Time: 15:53
 * Description: 业务类型操作说明
 */
@RestController
@RequestMapping("busiTypeArticle")
public class BusiTypeArticleController extends BaseController<BusiTypeArticleController> {

    /**
     * 更新文章
     * @param vo
     * @return
     * @throws Exception
     */
    @PostMapping("update")
    public ResponseData update(@RequestBody ArticleVo vo) throws Exception{
        BusinessTypeOptArticle article = new BusinessTypeOptArticle();
        article.setArticleContent(vo.getContent());
        article.setArticleTitle(vo.getTitle());
        article.setBusiType(vo.getBusiType());
        article.setId(vo.getId());
        busiTypeArticleService.update(article);
        return ResponseDataUtil.buildSuccess(article);
    }

    /**
     * 查询文章
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("detail/{id}")
    public ResponseData detail(@PathVariable("id") Integer id) throws Exception{
        return ResponseDataUtil.buildSuccess(busiTypeArticleService.getDataById(id));
    }

    /**
     * 获取列表
     * @return
     * @throws Exception
     */
    @GetMapping("list")
    public ResponseData getArticleList() throws Exception{
        return ResponseDataUtil.buildSuccess(busiTypeArticleService.getTitleList());
    }

    /**
     * 业务类型绑定操作说明文档
     * @param articleId
     * @param busiType
     * @return
     * @throws Exception
     */
    @GetMapping("bind/{articleId}/{busiType}")
    public ResponseData bindBusiType(@PathVariable("articleId") Integer articleId,
                                     @PathVariable("busiType") Integer busiType) throws Exception{
        List<BusinessTypeOptArticle> list = busiTypeArticleService.getDataByBusiType(busiType);
        if(!CollectionUtils.isEmpty(list)){
            // 清空绑定关系
            for(BusinessTypeOptArticle item : list){
                item.setBusiType("");
                busiTypeArticleService.update(item);
            }
        }
        BusinessTypeOptArticle article = busiTypeArticleService.getDataById(articleId);
        article.setBusiType(busiType.toString());
        busiTypeArticleService.update(article);
        return ResponseDataUtil.buildSuccess();
    }

    /**
     * 业务类型解绑操作说明文档
     * @param articleId
     * @return
     * @throws Exception
     */
    @GetMapping("unbind/{articleId}")
    public ResponseData unbindBusiType(@PathVariable("articleId") Integer articleId) throws Exception{
        BusinessTypeOptArticle article = busiTypeArticleService.getDataById(articleId);
        article.setBusiType("");
        busiTypeArticleService.update(article);
        return ResponseDataUtil.buildSuccess();
    }

    /**
     * 删除
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("delete/{id}")
    public ResponseData delete(@PathVariable("id") Integer id) throws Exception{
        BusinessTypeOptArticle article = busiTypeArticleService.getDataById(id);
        if (article == null){
            return ResponseDataUtil.buildError(ResultEnum.PARAM_ERROR);
        }
        if(StringUtils.isNotBlank(article.getBusiType())){
            return ResponseDataUtil.buildError(Constants.ERROR, "已进行绑定操作，不能进行删除！");
        }
        busiTypeArticleService.delete(id);
        return ResponseDataUtil.buildSuccess();
    }

    /**
     *
     * @param busiType
     * @return
     * @throws Exception
     */
    @GetMapping("busiType/{busiType}")
    public ResponseData getDataByBusiType(@PathVariable("busiType") Integer busiType) throws Exception{
        List<BusinessTypeOptArticle> list = busiTypeArticleService.getDataByBusiType(busiType);
        if(CollectionUtils.isEmpty(list)){
            return ResponseDataUtil.buildSuccess();
        }
        return ResponseDataUtil.buildSuccess(list.get(0));
    }
}
